package edu.gatech.oad;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class RegisterActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.a_register);

    	final EditText location= (EditText) findViewById(R.id.editLocation);
    	final EditText firstName= (EditText) findViewById(R.id.searchUser);
    	final EditText lastName= (EditText) findViewById(R.id.EditText01);

    	// email - handled as @Username in accordance to DataBaseHelper
    	// doesn't remove @email - should we do this? or should we add
    	// another separate username EditText field?
    	final EditText eMail = (EditText) findViewById(R.id.EditText02);
    	final EditText initialPass= (EditText) findViewById(R.id.EditText03);
    	final EditText confirmPass= (EditText) findViewById(R.id.EditText04);
    	final Button confirmRegistration = (Button) findViewById(R.id.finduserbutton);
        final Button cancelRegistration = (Button) findViewById(R.id.button2);
        
        // Listening to @confirmRegistration button to check data - 
        // if legal data pass to db and send to the Main Activity - 
        // if not legal throw appropriate errors and notify the user
        // (error messages not implemented yet - currently accepts
        // nulls etc - because I set the input text maybe it innately
        // does this - not sure yet)
        
        confirmRegistration.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		
        		// getting the data, casting to type String and setting to final String vars
        		final String _FIRST_NAME = firstName.getText().toString();
        		final String _LAST_NAME = lastName.getText().toString();
        		final String _EMAIL = eMail.getText().toString();
        		final String _INITIAL_PASS = initialPass.getText().toString();
        		final String _CONFIRM_PASS = confirmPass.getText().toString();
        		
        		if(_FIRST_NAME == null)
        			firstName.setError(getString(R.string.error_field_required));
        		if(_LAST_NAME == null)
        			lastName.setError(getString(R.string.error_field_required));        		
        		if(_EMAIL == null)
        			eMail.setError(getString(R.string.error_field_required));       		
        		if(_INITIAL_PASS == null)
        			initialPass.setError(getString(R.string.error_field_required));        			
        		if(_CONFIRM_PASS == null)
        			confirmPass.setError(getString(R.string.error_field_required));        			
        		if(!_INITIAL_PASS.equals(_CONFIRM_PASS))
        		{
        			confirmPass.setError(getString(R.string.error_mismatch));
        		}
	        	DataBaseHelper db = new DataBaseHelper(getApplicationContext());
	        	try {
	        		db.createDataBase();
	        		db.openDataBase();
	        	} catch(Exception sqle) {
	        		throw new Error("Unable to open database");
	        	}
	        	
	        	// additional errors/exception? 
	        	// such as checking if intialPass.equals(confirmPass)
	        	// @eMail = @Username
	        	// account @AccountStatus is set to "Active"
	        	// account @Type is set to "User"
	        	db.registerUser(_EMAIL, _CONFIRM_PASS, _FIRST_NAME, _LAST_NAME, "Active", "Reg");
	        	
	        	// send authentication email?
	        	
	        	// login in acc and send to main or send back to login
	        	
	        	// for now send back to login w/ no confirm email
	        	confirmRegistration.setOnClickListener(new View.OnClickListener() {
	   			 
	                public void onClick(View v) {
	                    // Switching to Register screen
	                    Intent a = new Intent(getApplicationContext(), MainActivity.class);
	                    startActivity(a);
	                }
	            });
        	}
        });
 
        // Listening to @cancelRegistration button to return cancel and
        // return to login page
        cancelRegistration.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View v) {
            	
            	// Closing registration screen
                // Switching to Login Screen/closing register screen
            	finish();
            	
            	// If above doesn't work use below and remove finish();
            	/*
            	 * Intent a = new Intent(getApplicationContext(), LoginActivity.class);
            	 * startActivity(a);
            	 * 
            	 */
            	
            }
        });
    }
}